<?php

/**
 * 账户提现
 */

namespace app\member\common;

class CashCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $this->setMeta('账户提现');
        $this->pageInfo['name'] = '账户提现';

        $urlParam = [];
        $where = [];
        $type = request('get', 'type', 0, 'intval');

        $urlParam['type'] = $type;
        if ($type == 1) {
            $where['A.status'] = 1;
        }
        if ($type == 2) {
            $where['A.status'] = 2;
        }
        if ($type == 3) {
            $where['A.status'] = 0;
        }

        $pageLimit = 10;
        $where['A.user_id'] = $this->userInfo['user_id'];

        $model = target('member/PayCash');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'create_time desc');

        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->assign('pageParams', $urlParam);
        $this->assign('type', $type);
        $this->memberDisplay();
    }

    public function info() {
        $cashNo = request('', 'no');
        $info = target('member/PayCash')->getWhereInfo([
            'cash_no' => $cashNo
        ]);
        if (empty($info)) {
            $this->error('该订单不存在!');
        }

        $this->pageInfo['name'] = '提现申请';
        $this->setMeta('提现申请');
        $this->setCrumb([
            [
                'name' => '提现申请',
                'url' => url()
            ]
        ]);
        $this->assign('info', $info);
        $this->memberDisplay();
    }

    public function submit() {
        $cardInfo = target('member/PayCard')->getWhereInfo(['A.user_id' => $this->userInfo['user_id']]);
        if(empty($cardInfo)) {
            $this->redirect(url('member/card/index'));
            exit;
        }
        if(!isPost()) {
            $this->pageInfo['name'] = '提现申请';
            $this->setMeta('提现申请');
            $this->setCrumb([
                [
                    'name' => '提现申请',
                    'url' => url()
                ]
            ]);
            preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $cardInfo['account'],$match);
            unset($match[0]);
            $cardInfo['account'] = implode(' ', $match);
            $this->assign('cardInfo',$cardInfo);
            $this->memberDisplay();
        }else {
            $money = request('post', 'money', 0, 'intval');
            if(empty($money)) {
                $this->error('请输入提现金额！');
            }
            if($money > $this->userInfo['money'] || $money <= 0) {
                $this->error('可提现金额不足！');
            }
            if(empty($cardInfo)) {
                $this->error('请先添加银行卡！');
            }


            $config = target('member/MemberConfig')->getConfig();


            if($money < $config['clear_withdraw']) {
                $this->error('最少提现额度为'.$config['clear_withdraw'] . '元');
            }

            if($config['clear_num']) {
                $count = target('member/PayCash')->countList(['_sql' => 'A.status != 0 AND A.create_time >= ' .mktime(0,0,0,date('m'),1,date('Y')) . ' AND A.create_time <= ' . mktime(23,59,59,date('m'),date('t'),date('Y'))]);
                if($count >= $config['clear_num']) {
                    $this->error('当月提现次已满，请于下月继续提现！');
                }
            }


            $cashNo = target('order/Order', 'service')->logNo();
            $data = [
                'cash_no' => $cashNo,
                'user_id' => $this->userInfo['user_id'],
                'money' => $money,
                'create_time' => time(),
                'status' => 1,
                'bank' => $cardInfo['name'],
                'label' => $cardInfo['label'],
                'tax' => $config['clear_tax'],
                'account' => $cardInfo['account'],
                'account_tel' => $cardInfo['account_tel'],
                'account_name' => $cardInfo['account_name'],
                'account_bank' => $cardInfo['account_bank'],
            ];
            target('member/PayCash')->beginTransaction();
            if(!target('member/PayCash')->add($data)) {
                target('member/PayCash')->rollBack();
                $this->error('提现申请失败!');
            }
            $status = target('member/Finance', 'service')->account([
                'user_id' => $this->userInfo['user_id'],
                'money' => $money,
                'pay_no' => $cashNo,
                'pay_name' => '账户提现',
                'type' => 0,
                'deduct' => 1
            ]);
            if(!$status) {
                target('member/PayCash')->rollBack();
                $this->error(target('member/Finance', 'service')->getError());
            }

            target('member/PayCash')->commit();
            $this->success('提现申请成功！', url('index'));
        }
    }

}